package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;


public class FuyinshibaoNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://fuyinshibao.cn/[a-z0-9/]*/[%A-Z0-9\\-]*"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title=doc.select("div.a-header h1").text();
		if(StringUtils.isBlank(title))
			title=doc.select("h1.siz-30").text();
		date=DateUtils.matchDate(doc.select("div.ah-sub").text());
		if(date==null)
			date=DateUtils.matchDate(doc.select("span.ar-time").text());
		content =HtmlCleaner.getContentHtml(url, doc.select("div.a-style-smallimg"));
		if(StringUtils.isBlank(content))
			content =HtmlCleaner.getContentHtml(url, doc.select("div.ar-right"));
		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	public static void main(String[] args) {
		FuyinshibaoNewsAnalyse analyse=new FuyinshibaoNewsAnalyse();
		String url="http://fuyinshibao.cn/news/37590/%E7%88%B1%E5%BE%B7%E5%9F%BA%E9%87%91%E4%BC%9A%E8%A5%BF%E5%8C%97%E5%96%84%E7%89%A7%E8%AE%A1%E5%88%92%E7%AC%AC%E4%B8%80%E6%9C%9F%E6%88%90%E9%95%BF%E8%90%A5%E5%9C%A8%E6%B8%AD%E5%8D%97%E5%B8%82%E5%9F%BA%E7%9D%A3%E6%95%99%E4%B8%A4%E4%BC%9A%E9%A1%BA%E5%88%A9%E5%BC%80%E7%8F%AD";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}

